package com.jubao.ejb.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.ProductPriceDaoLocal;
import com.jubao.ejb.dao.ProductPriceDaoRemote;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.dao.util.DaoUtil;
import com.jubao.ejb.entity.ProductPrice;

@Stateless
@LocalBinding(jndiBinding = DaoConstant.ProductPriceDaoLocalJndiName)
@RemoteBinding(jndiBinding = DaoConstant.ProductPriceDaoRemoteJndiName)
public class ProductPriceDaoImpl implements ProductPriceDaoLocal, ProductPriceDaoRemote {
	static Logger logger = Logger.getLogger(ProductPriceDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -7861568656574099969L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public void setEm(EntityManager em) {
		this.em = em;
	}

	public ProductPrice find(long sysId) {
		// TODO Auto-generated method stub
		return em.find(ProductPrice.class, sysId);
	}

	public ProductPrice find(long storeSysId, int relationId, long productSysId) {
		if (storeSysId > -1 && relationId > -1 && productSysId > -1) {
			List<ProductPrice> pps = this.getProductPriceList(-1, -1, storeSysId, relationId, productSysId, 0, 1);
			if (pps != null && pps.size() > 0) {
				return pps.get(0);
			}
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public List<ProductPrice> getProductPriceList(int relationIdAreaCat, long areaCategorySysId, long storeSysId,
			int relationId, long productSysId, int pgno, int pgsize) {
		List<ProductPrice> result = new ArrayList<ProductPrice>(0);
		Query q = this.composeGetProductPriceQuery(relationIdAreaCat, areaCategorySysId, storeSysId, relationId,
				productSysId, true);
		//.setHint("org.hibernate.cacheRegion",	"/entity/Category/RelatedCategory/List")
		List rs = DaoUtil.getResultList(q, pgno, pgsize);
		return rs;

	}

	public long getProductPriceCount(int relationIdAreaCat, long areaCategorySysId, long storeSysId, int relationId,
			long productSysId) {
		Query q = this.composeGetProductPriceQuery(relationIdAreaCat, areaCategorySysId, storeSysId, relationId,
				productSysId, false);
		Long result = (Long) q.getSingleResult();
		return result;
	}

	private Query composeGetProductPriceQuery(int relationIdAreaCat, long areaCategorySysId, long storeSysId,
			int relationId, long productSysId, boolean isGetList) {
		String ent = isGetList ? "pp" : "count(pp)";

		String eql = "select " + ent + " from  ProductPrice pp";
		String whereand = "";
		if (relationIdAreaCat > -1 || areaCategorySysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += ", GeneRelation gr " + whereand + " pp.store = gr.entity";
		}
		if (storeSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  pp.store.sysId=:storeSysId ";
		}
		if (relationId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  pp.relationId=:relationId ";
		}
		if (productSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  pp.product.sysId=:productSysId ";
		}

		if (relationIdAreaCat > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  gr.relationId=:relationIdAreaCat";
		}
		if (areaCategorySysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  gr.entity2.sysId=:areaCategorySysId";
		}

		eql +=" ORDER BY pp.createdDate DESC";
		
		logger.info("eql=[" + eql + "]");

		Query q = em.createQuery(eql);
		if (storeSysId > -1) {
			q.setParameter("storeSysId", storeSysId);
		}
		if (relationId > -1) {
			q.setParameter("relationId", relationId);
		}
		if (productSysId > -1) {
			q.setParameter("productSysId", productSysId);
		}
		if (relationIdAreaCat > -1) {
			q.setParameter("relationIdAreaCat", relationIdAreaCat);
		}
		if (areaCategorySysId > -1) {
			q.setParameter("areaCategorySysId", areaCategorySysId);
		}
		return q;
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public ProductPrice save(ProductPrice productPrice) {
		return em.merge(productPrice);
	}

}
